package cn.lrwjz.androidguide.ui;

import android.os.Bundle;
import android.view.MenuItem;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.google.android.material.bottomappbar.BottomAppBar;
import com.google.android.material.floatingactionbutton.FloatingActionButton;

import cn.lrwjz.androidguide.R;

public class BottomAppBarActivity extends AppCompatActivity {
    private BottomAppBar bottomAppBar;
    private FloatingActionButton fab;
    private boolean isFabAlignedEnd = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bottom_app_bar);

        // 设置工具栏
        androidx.appcompat.widget.Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setTitle("BottomAppBar 示例");
        }

        // 初始化 BottomAppBar
        bottomAppBar = findViewById(R.id.bottomAppBar);
        fab = findViewById(R.id.fab);

        // 设置导航图标点击事件
        bottomAppBar.setNavigationOnClickListener(v -> 
            Toast.makeText(this, "点击了导航菜单", Toast.LENGTH_SHORT).show());

        // 设置菜单项点击事件
        bottomAppBar.setOnMenuItemClickListener(item -> {
            int itemId = item.getItemId();
            if (itemId == R.id.action_search) {
                Toast.makeText(this, "点击了搜索", Toast.LENGTH_SHORT).show();
                return true;
            } else if (itemId == R.id.action_favorite) {
                Toast.makeText(this, "点击了收藏", Toast.LENGTH_SHORT).show();
                return true;
            } else if (itemId == R.id.action_more) {
                Toast.makeText(this, "点击了更多", Toast.LENGTH_SHORT).show();
                return true;
            }
            return false;
        });

        // 设置FAB点击事件
        fab.setOnClickListener(v -> {
            // 切换FAB的对齐方式
            isFabAlignedEnd = !isFabAlignedEnd;
            bottomAppBar.setFabAlignmentMode(
                isFabAlignedEnd ? 
                BottomAppBar.FAB_ALIGNMENT_MODE_END : 
                BottomAppBar.FAB_ALIGNMENT_MODE_CENTER
            );
            
            Toast.makeText(this, 
                "FAB位置：" + (isFabAlignedEnd ? "右侧" : "中间"), 
                Toast.LENGTH_SHORT).show();
        });
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
} 